<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <eHeader :permission="permission" />
      <crudOperation :permission="permission" :async="true" />
    </div>
    <!--表格渲染-->
    <el-table ref="table" v-loading="crud.loading" :data="crud.data" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
      <el-table-column type="selection" width="55" />
      <el-table-column prop="username" label="用户名" />
      <el-table-column prop="fullName" label="姓名" />
      <el-table-column prop="sex" label="性别">
        <template slot-scope="scope">
          {{ scope.row.sex===1?'男':'女' }}
        </template>
      </el-table-column>
      <el-table-column prop="email" label="邮箱" />
      <el-table-column prop="mobile" label="手机" />
      <el-table-column prop="status" label="状态" align="center">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            :active-value="1"
            :inactive-value="0"
            active-color="#409EFF"
            inactive-color="#F56C6C"
            @change="changeEnabled(scope.row, scope.row.status)"
          />
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建日期" />
      <!--   编辑与删除   -->
      <el-table-column
        v-if="checkPer(['sys:user:update','sys:user:delete'])"
        label="操作"
        width="130px"
        align="center"
        fixed="right"
      >

        <template slot-scope="scope">
          <udOperation
            :data="scope.row"
            :permission="permission"
            async
          />
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <pagination />
    <!--表单渲染-->
    <eForm />
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import crudUser from '@/api/sys/user'
import eHeader from './module/header'
import eForm from './module/form'
import CRUD, { presenter } from '@crud/crud'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'
import udOperation from '@crud/UD.operation'

const addPageUrl = '/sys/user/add';
const editPageUrl = '/sys/user/edit/';

export default {
  name: 'User',
  components: { eHeader, eForm, crudOperation, pagination, udOperation },
  cruds() {
    return CRUD({
      title: '用户',
      url: 'sys/user/list',
      crudMethod: { ...crudUser },
      idField: "userId",
      addPage: true,
      addPageUrl: addPageUrl,
      editPage: true,
      editPageUrl: editPageUrl
    })
  },
  mixins: [presenter()],
  // 数据字典

  data() {
    return {
      permission: {
        add: ["sys:user:save"],
        edit: ["sys:user:update"],
        del: ["sys:user:delete"],
      }
    }
  },

  methods: {
    // 改变状态
    changeEnabled(data, val) {
      this.$confirm('此操作将 ' + data.username + '用户禁用, 是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // eslint-disable-next-line no-undef
        crudUser.edit(data).then(() => {
          // eslint-disable-next-line no-undef
          this.crud.notify('成功', 'success')
        }).catch(err => {
          data.status = !data.status
          console.log(err.data.msg)
        })
      }).catch(() => {
        data.status = !data.status
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
 ::v-deep .el-input-number .el-input__inner {
    text-align: left;
  }
</style>
